<?php
/*
	用户端公共文件，访问用户端时默认加载
*/
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'index';
if($_RQ['op']=='index'){
	//还不是分销商，跳转申请页
	$return['member']=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($_RQ['level_id']){
		$apply=$this->get('apply',array('level_id'=>$_RQ['level_id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		$apply['level_id']=$_RQ['level_id'];
		if($_RQ['level_id']>$return['member']['level_id']){
			$apply['status']=0;
			$apply['ispay']=0;
		}
	}elseif($return['member']['is_agent']){
		$apply=$this->get('apply',array('level_id >'=>$return['member']['level_id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		$apply['level_id']=$return['member']['level_id']+1;
	}else{
		$apply=$this->get('apply',array('level_id'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	if($apply['id']){
		if($apply['fee']>0 && !$apply['ispay']){
			$apply['needpay']=1;
		}
	}
	$apply['redpcd']=json_decode($apply['redpcd'],true);
	if(!$return['member']['mid']){
		$this->model('member')->register();
	}
	$return['become']=$this->setting['become'];
	$return['page_setting']=$this->plugin_setting('page');
	$return['apply_setting']=$this->plugin_setting('apply');
	if(!$this->setting['no_indexurl']){
		$return['index_url']=$this->setting['index_url']?:'jy_weishop/index/';
	}
	if($return['member']['is_agent']){
		$apply['province']=$apply['province']?:$return['member']['province'];
		$apply['city']=$apply['city']?:$return['member']['city'];
		$apply['realname']=$apply['realname']?:$return['member']['realname'];
		$apply['mobile']=$apply['mobile']?:$return['member']['mobile'];
		$apply['is_agent']=1;
	}else{
		$member=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));	
		$apply['realname']=$apply['realname']?:$member['realname'];
		$apply['mobile']=$apply['mobile']?:$member['mobile'];
		$apply['wechat']=$apply['wechat']?:$member['wechat'];
	}
	$return['apply']=$apply;
	$return['apply']['oldmobile']=$return['apply']['mobile'];
	$return['apply']['area']=$return['apply']['area']?json_decode($return['apply']['area'],true):array('duodai'=>'');
	$return['apply']['level_type']=$return['apply']['level_id']?:'0';
	$return['province']=$this->model()->available_areas();
	$return['level_types']=$this->setting['fenset'];
	unset($return['level_types'][3]);
	foreach($return['level_types'] as $key=>$title){
		if($_RQ['level_id'] && $_RQ['level_id']!=$key){
			unset($return['level_types'][$key]);
		}
		if($return['member']['is_agent'] && $return['member']['level_id']>=$key){
			unset($return['level_types'][$key]);
		}
	}
	$return['apply']['openfee']=$return['level_types'][$return['apply']['level_type']]['openfee'];
	$return['xieyi'][0]=$this->plugin_setting('xieyi0');
	$return['xieyi'][1]=$this->plugin_setting('xieyi1');
	$return['xieyi'][2]=$this->plugin_setting('xieyi2');
	if(!$return['apply_setting']['is_tan'] || !$return['xieyi'][$return['apply']['level_type']] || ($return['apply']['id'] && !$apply['needpay'])){
		$return['apply']['agree']=1;
	}
	$checkapptype=$this->model('member')->checkapptype(1);
	$return['apptype']=$checkapptype['apptype'];
	if($return['apptype']==1){
		$return['apply']['province2']=$return['apply']['province']=$checkapptype['boss']['province'];
		$return['apply']['city2']=$return['apply']['city']=$checkapptype['boss']['city'];
	}
	set_html('title',$return['apply_setting']['html_title']?:'申请入驻');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['level_type'] && $post['province2'] && $post['city2']){
		$post['province']=$post['province2'];
		$post['city']=$post['city2'];
	}
	if(!$post['level_type'] && $this->setting['is_needcity'] && $post['province'] && $post['city']){
		$params=array('is_agent'=>1,'uniacid'=>$_SESSION['uniacid']);
		$params['province']=$post['province'];
		$params['city']=$post['city'];
		$params['level_id']=1;
		$iscity=pdo_count('jy_ylfee_member',$params);
		if(!$iscity){
			exi('需要已经有市代城市才能申请股东！请选择其他城市！','error');
		}
	}
	if(!$post['level_type'] && $this->setting['agent0num']  && $post['province'] && $post['city']){
		$params=array('is_agent'=>1,'uniacid'=>$_SESSION['uniacid']);
		$params['province']=$post['province'];
		$params['city']=$post['city'];
		$params['level_id']=0;
		$iscity=pdo_count('jy_ylfee_member',$params);
		if($iscity>=$this->setting['agent0num']){
			exi('该市股东人数已满！请选择其他城市！','error');
		}
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if($post['login_type']>0 && $post['oldmobile']!=$post['mobile'] && !check_sms_code($post['smscode'])){
		$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'mobile');
		if($post['mobile']!=$oldmobile){
			exi('手机验证码错误！','error');
		}
	}
	if($post['level_type']==2 && !$post['province']){
		$post['city']='';
		exi('请选择代理的省！','error');
	}
	if($post['level_type']<=1 && !$post['city']){
		exi('请选择代理的城市！','error');
	}
	if($post['level_type']<=1 && !$post['redpcd']){
		exi('请选择邮寄区域！','error');
	}
	if(!$post['is_agent'] && $post['level_type']<=1 && !$post['address']){
		exi('请输入邮寄的详细地址！','error');
	}
	$post['area']=array('province'=>$post['province'],'city'=>$post['city']);
	if($post['openfee']>0){
		//独家代理模式下，不允许重复
		$this->model('member')->check_isadd($post);
		$pay_id=$this->getcolumn('apply',array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']),'pay_id');
		$area_code=$this->model()->get_area_code($post['area'],$post['level_type']);
		if(!$post['level_type'] && $this->setting['level0koufee']>0){
			$jifee=$post['openfee']-$this->setting['level0koufee'];
		}else{
			$jifee=$post['openfee'];
		}
		$new_apply=array(
				'payfee'=>$post['openfee'],
				'fee'=>$jifee>0?$jifee:0,
				'mobile'=>trim($post['mobile']),
				'realname'=>trim($post['realname']),
				'wechat'=>trim($post['wechat']),
				'area_type'=>pdo_getcolumn('core_area',array('code'=>$area_code),'type'),
				'area_code'=>$area_code,
				'province'=>$post['province'],
				'city'=>$post['city'],
				'level_id'=>$post['level_type'],
				'redpcd'=>$post['level_type']<=1?ijson_encode($post['redpcd']):'',
		);
		if(!$pay_id){
			$new_apply['uniacid']=$_SESSION['uniacid'];
			$new_apply['mid']=$_SESSION['uid'];
			$new_apply['status']=0;
			$new_apply['createtime']=TIMESTAMP;
			$this->insert('apply',$new_apply);
			$apply_id=$this->insertid();
			$params=array(
				'action'=>'apply',
				'id'=>$apply_id,
				'fee'=>$post['openfee'],
				'title'=>'申请成为代理商',
				'plugin'=>'jy_ylfee',
				//'pay_type'=>'wechat',
				'no_type'=>array('hdfk'),
				'back_url'=>'jy_ylfee/apply/pay_result',
				'success_url'=>'jy_ylfee/apply/',
				'error_url'=>'jy_ylfee/apply/',
			);
			if($_SESSION['container']=='aliapp' && $_SESSION['aliappopenid']){
				$params['pay_type']='alipay';
			}
			$pay_id=cfc('pay')->create_pay_order($params);
			$this->update('apply',array('pay_id'=>$pay_id),array('id'=>$apply_id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$this->update('apply',$new_apply,array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
			pdo_update('core_paylog',array('fee'=>$post['openfee']),array('id'=>$pay_id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi($pay_id);
	}else{
		$area_code=$this->model()->get_area_code($post['area'],$post['level_type']);
		$this->model('member')->apply_agent($_SESSION['uid'],$area_code,$post);
		$apptype=$this->model('member')->checkapptype();
		if($apptype==2 && !$post['level_type']){
			pdo_update('core_members',array('fromuser'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']));
			pdo_update('jy_ylfee_member',array('fromuser'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		}
		exi('申请成功！请耐心等待管理员审核！');
	}
}
if($_RQ['op']=='pay_result'){
	$payresult=$_SESSION['pay_result'];
	$apply=pdo_get('jy_ylfee_apply',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if($apply['payfee']!=$payresult['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($apply['ispay']){
		exi('该订单已支付！无需重复支付！','error');
	}
	pdo_update('jy_ylfee_apply',array('ispay'=>1,'paytime'=>time()),array('id'=>$apply['id'],'uniacid'=>$_SESSION['uniacid']));
	$this->model('member')->check_agent($apply['id'],1);
	$apptype=$this->model('member')->checkapptype();
	if($apptype==2 && !$apply['level_id']){
		pdo_update('core_members',array('fromuser'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']));
		pdo_update('jy_ylfee_member',array('fromuser'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	$apply['price']=$apply['fee'];
	$apply['price']=$apply['price']>0?$apply['price']:1;
	$apply['ordersn']='apply'.$apply['id'];
	$apply['pcd']=array('province'=>$apply['province'],'city'=>$apply['city']);
	$apply['address']='';
	if($apply['level_id']<=1 && $apply['redpcd']){
		$apply['redpcd']=json_decode($apply['redpcd'],true);
	}
	$this->model('commission')->create($apply,1);
	exi('申请成功！');
}